<template>
  <div class="container">
    <div class="app-container">
      <div class="">
        <!-- 角色管理内容 -->
        <div class="role-operate">
          <el-button size="mini" type="primary" @click="dialogVisible =true">添加角色</el-button>
        </div>
      </div>
      <div class="role-oper">
        <template>
          <el-table :data="getRoleAPIList" border style="width: 100%">
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="角色" width="180">
              <template v-slot="{row}">
                <!-- 根据isEdit 来判断显示不同dom结构 -->
                <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
                <span v-else>{{ row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="state" label="启用" width="300" :formatter="formatter">
              <template v-slot="{ row }">
                <el-switch v-if="row.isEdit" v-model="row.editRow.state" :active-value="1" :inactive-value="0" />
                <span v-else>{{ row.state === 1 ? "已启用" : "未启用" }}</span>
              </template>
            </el-table-column>

            <el-table-column prop="description" label="描述">
              <template v-slot="{ row }">
                <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
                <span v-else>{{ row.description }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作">

              <template v-slot="{ row }">
                <template v-if="row.isEdit">
                  <!-- 编辑状态 -->
                  <el-button type="primary" size="mini" @click="enitAPI(row)">确定</el-button>
                  <el-button size="mini" @click="enit1(row)">取消</el-button>
                </template>
                <template v-else>
                  <!-- 非编辑状态 -->
                  <el-button size="mini" type="text">分配权限</el-button>
                  <el-button size="mini" type="text" @click="enit(row)">编辑</el-button>
                  <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
                </template>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </div>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :page-size="params.pagesize"
          layout="prev, pager, next"
          :total="total"
          @current-change="currentChange"
        />
      </el-row>
      <!-- 弹框 -->
      <el-dialog width="500px" title="新增角色" :visible.sync="dialogVisible" @close="close">
        <!-- 表单内容 -->
        <el-form ref="addForm" label-width="120px" :model="addForm" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="addForm.name" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item label="启用" prop="state">
            <el-switch v-model="addForm.state" size="mini" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="addForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
          </el-form-item>
          <el-form-item>
            <el-row type="flex" justify="center">
              <el-col :span="12">
                <el-button type="primary" size="mini" @click="confirmAdd">确定</el-button>
                <el-button size="mini" @click="dialogVisible=false">取消</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, enitAPI, delRole } from '@/api/role'

export default {
  name: 'Role',
  data() {
    return {
      params: {
        page: 1,
        pagesize: 5
      },
      getRoleAPIList: [],
      total: 0,
      dialogVisible: false,
      addForm: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      isEdit: false
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取数据
    async getRoleList() {
      const res = await getRoleList(this.params)
      console.log(res)

      this.getRoleAPIList = res.rows
      this.total = res.total
      // 针对每一行数据添加一个编辑标记
      this.getRoleAPIList.forEach(item => {
        // item.isEdit = false // 添加一个属性 初始值为false
        // 数据响应式的问题  数据变化 视图更新
        // 添加的动态属性 不具备响应式特点
        // this.$set(目标对象, 属性名称, 初始值) 可以针对目标对象 添加的属性 添加响应式
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    // 启用格式化
    formatter(type) {
      const ENT = {
        0: '未启用',
        1: '已启用'
      }
      return ENT[type.state]
    },
    // 分页器
    currentChange(page) {
      this.params.page = page
      this.getRoleList()
    },
    // 清空
    close() {
      this.$refs.addForm.resetFields()
      this.dialogVisible = false
      this.addForm = {
        name: '',
        description: '',
        state: 0
      }
    },
    // 创建
    async confirmAdd() {
      await this.$refs.addForm.validate()
      await addRole(this.addForm)
      this.$message.success('创建成功')
      this.close()
      this.getRoleList()
    },
    // 编辑
    enit(row) {
      console.log(row)
      row.isEdit = true
      row.editRow.name = row.name
      row.editRow.state = row.state
      row.editRow.description = row.description
    },
    enit1(row) {
      Object.assign(row, {
        isEdit: false // 退出编辑模式
      })
    },
    async enitAPI(row) {
      if (row.editRow.name && row.editRow.description) {
        await enitAPI({ ...row.editRow, id: row.id })
        this.$message.success('更新成功')
        Object.assign(row, {
          ...row.editRow,
          isEdit: false // 退出编辑模式
        })
      } else {
        this.$message.success('不能为空')
      }
    },
    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRole(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getRoleList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }

}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
.role-oper{
  padding: 10px;
}
</style>
